<template>
    <div>
        <Card>
            <!-- search start -->
            <div class="search">
                <Form :model="search" class="search_box" inline label-position="right">

                    <FormItem label="OPENID">
                        <Input v-model="search.openid" style="width:300px" class="FormItemInput"></Input>
                    </FormItem>

                    <FormItem label="分部 -> 楼层 -> 区域(可输入)：">
                        <Cascader filterable class="cascader" :value="cascadeArr" :data="cascadeList" change-on-select @on-change="cascadeSelection" :clearable="false"></Cascader>
                    </FormItem>

                    <FormItem label="编号：">
                        <Input v-model="search.number" style="width:100px" class="FormItemInput"></Input>
                    </FormItem>

                    <FormItem label="时间：">
                        <DatePicker style="width:300px" type="datetimerange" placeholder="Select date and time" @on-change="changeTime($event)"></DatePicker>
                    </FormItem>

                    <FormItem>
                        <Button icon="md-search" type="primary" @click="search_list">搜索</Button>
                    </FormItem>

                </Form>

            </div> <!-- end search -->

            <Table :loading="loading" stripe :columns="columns1" :data="data1"></Table>
            <Page :total="total" :current="search.page" show-sizer show-total :page-size="search.rows" @on-page-size-change="get_list_by_rows" @on-change="get_list_by_page"> </Page>

        </Card>
    </div>
</template>

<script>
    import axios from '@/libs/api.request'
    import {
        selectDepartment
    } from '@/libs/tools'
    import {
        mapGetters,
        mapMutations
    } from 'vuex'
    export default {
        name: 'msg-detail',
        data() {
            return {
                // 加载状态
                loading: false,

                // 级联回显数组
                cascadeArr: [-1],

                // 搜索对象
                search: {
                    branchid: -1,
                    floorid: -1,
                    regionid: -1,
                    openid: '',
                    number: '',
                    start_time: '',
                    end_time: '',
                    page: 1,
                    rows: 10
                },

                // 数据总条数
                total: 0,


                columns1: [{
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: 'OPENID',
                        align: 'center',
                        key: 'openid'
                    },
                    {
                        title: '昵称',
                        align: 'center',
                        key: 'nickname'
                    },
                    {
                        title: '联系电话',
                        align: 'center',
                        key: 'tel'
                    },
                    {
                        title: '分部',
                        align: 'center',
                        key: 'branch_zw'
                    },
                    {
                        title: '楼层',
                        align: 'center',
                        key: 'floor_zw'
                    },
                    {
                        title: '工位编号',
                        align: 'center',
                        key: 'number',
                    },
                    {
                        title: '留言时间',
                        align: 'center',
                        key: 'c_time'
                    },
                    {
                        title: '留言内容',
                        align: 'center',
                        key: 'message'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 100,
                        align: 'center',
                        render: (h, params) => {
                            if (this.role < 3) {
                                return h('div', [
                                    h('i-switch', {
                                        props: {
                                            type: 'primary',
                                            value: params.row.enable == 1, // 控制开关的打开或关闭状态，官网文档属性是value
                                            size: 'large',
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            'on-change': (value) => { // 触发事件是on-change,用双引号括起来，
                                                // 参数value是回调值，并没有使用到
                                                let id = params.row.id // params.index是拿到table的行序列，可以取到对应的表格值
                                                // let status = 0
                                                let url = 'admin-work_station_message-ajax_on_wsm'
                                                if (value) {
                                                    // status = 1
                                                    this.switch(id, url)

                                                } else {
                                                    url = 'admin-work_station_message-ajax_off_wsm'
                                                    this.switch(id, url)
                                                }
                                            }
                                        }
                                    }, [
                                        h('span', {
                                            slot: 'open',
                                            domProps: {
                                                innerHTML: '显示',

                                            }
                                        }),
                                        h('span', {
                                            slot: 'close',
                                            domProps: {
                                                innerHTML: '屏蔽'
                                            }
                                        })
                                    ])
                                ])
                            }
                        }
                    },
                ],
                data1: [],
            }
        },
        methods: {
            ...mapGetters(['getRole', 'getBranchList']),

            // 获取留言明细列表
            get_msg_detail_list() {
                this.loading = true
                let {
                    ...data
                } = this.search
                axios.ajax_post('admin-work_station_message-ajax_get_wsm', data).then(res => {
                    this.loading = false
                    if (res.data.code === 1) {
                        this.data1 = res.data.data.list
                        this.total = res.data.data.total_num

                        if (this.data1.length < 1 && this.total > 0) {
                            this.search.page = Math.ceil(this.total / this.search.rows)
                            this.get_msg_detail_list()
                        }
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })
            },

            // 格式化时间
            changeTime(value) {
                this.search.start_time = value[0]
                this.search.end_time = value[1]
            },
            // 级联选择
            cascadeSelection(value) {

                value = value.map(Number)

                this.cascadeArr = value
                if (value.length == 1) {
                    this.search.branchid = value[0]
                    this.search.floorid = -1
                    this.search.regionid = -1
                    return false
                }
                if (value.length == 2) {
                    this.search.branchid = value[0]
                    this.search.floorid = value[1]
                    this.search.regionid = -1
                    return false
                }
                this.search.branchid = value[0]
                this.search.floorid = value[1]
                this.search.regionid = value[2]
            },
            // 显示/屏蔽
            switch (id, url) {
                let data = {
                    wsmid: id
                }
                axios.ajax_post(url, data).then(res => {
                    if (res.data.code === 1) {
                        this.$Notice.success({
                            title: res.data.msg,
                            duration: 3
                        })
                        this.get_msg_detail_list()
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })
            },

            // 搜索
            search_list() {
                this.search.page = 1
                this.search.rows = 10
                this.get_msg_detail_list()
            },

            get_list_by_rows(rows) {
                this.search.rows = rows
                this.get_msg_detail_list()
            },

            get_list_by_page(page) {
                this.search.page = page
                this.get_msg_detail_list()
            },
        },
        computed: {

            cascadeList() {
                let list = JSON.parse(JSON.stringify(this.getBranchList()))
                list.unshift({
                    value: -1,
                    label: '全部'
                })
                return list
            },
            role() {
                return this.getRole()
            }
        },
        created() {
            this.get_msg_detail_list()
        },
    }
</script>

<style lang="less" scoped>
    .search {
        .search_box {
            display: flex;
            flex-wrap: wrap;

            .option {
                width: auto;
            }

            .ivu-form-item {
                margin-right: 12px;
                display: flex;
                justify-content: flex-start;
                margin-bottom: 20px;
            }
        }
    }
</style>